Scripting App 中的 Notification 模块用于安排、管理和展示本地通知,支持多种触发方式、交互操作按钮和富交互界面(自定义 UI)。
使用 Notification.schedule 来安排本地通知。它支持标题、触发器、点击行为、操作按钮、自定义 UI 和其他投递选项:
| 参数名 | 类型 | 说明 |
|---|---|---|
title |
string |
必填,通知标题。 |
subtitle |
string? |
可选,副标题内容。 |
body |
string? |
可选,正文内容。 |
badge |
number? |
可选,应用图标角标数字。 |
silent |
boolean? |
可选,默认为 false。设为 true 则不播放声音静默送达。 |
interruptionLevel |
"active" | "passive" | "timeSensitive" |
可选,通知的重要级别和投递优先级。 |
userInfo |
Record<string, any>? |
可选,附加的自定义数据。 |
threadIdentifier |
string? |
可选,用于通知分组的标识符。 |
trigger |
TimeIntervalNotificationTrigger | CalendarNotificationTrigger | LocationNotificationTrigger | null |
可选,定义何时发送通知。 |
actions |
NotificationAction[]? |
可选,通知展开后展示的操作按钮。 |
customUI |
boolean? |
可选,设为 true 可使用 notification.tsx 自定义 UI。 |
tapAction |
"none" | { type: "runScript", scriptName: string } | { type: "openURL", url: string } |
可选,定义用户点击通知时执行的操作。 |
avoidRunningCurrentScriptWhenTapped |
boolean? |
已废弃,请改用 tapAction: "none"。 |
注意:
triggerTime和repeatsType参数已废弃,请统一使用trigger。
tapAction)通过 tapAction 参数,你可以完全控制用户点击通知时的行为:
"none":点击后无任何响应{ type: "runScript", scriptName: string }:运行指定脚本{ type: "openURL", url: string }:打开指定 URL,可为 deeplink 或 https 链接如果不设置 tapAction,默认行为是运行当前脚本,你可以通过 Notification.current 获取通知内容。
在指定秒数后触发通知:
timeInterval: 延迟秒数repeats: 是否重复触发nextTriggerDate(): 返回下次预期触发的时间根据特定日期和时间触发通知:
year、month、day、hour 等当进入或离开某个地理区域时触发:
通过 actions 参数添加通知操作按钮:
Script.createRunURLScheme(...) 创建 URL你可以使用 TSX 文件定义通知的展开视图:
customUI: truenotification.tsx 文件Notification.present(<JSX>) 渲染 UINotification.present(element: JSX.Element): void在 notification.tsx 中调用,用于渲染富通知界面。
notification.tsx| 方法名 | 说明 |
|---|---|
getAllDelivereds() |
获取所有已送达的通知 |
getAllPendings() |
获取所有已安排但尚未送达的通知 |
removeAllDelivereds() |
移除所有已送达的通知 |
removeAllPendings() |
取消所有待发送通知 |
removeDelivereds(ids) |
移除指定 ID 的已送达通知 |
removePendings(ids) |
取消指定 ID 的已安排通知 |
getAllDeliveredsOfCurrentScript() |
获取当前脚本发送的已送达通知 |
getAllPendingsOfCurrentScript() |
获取当前脚本安排的待发送通知 |
removeAllDeliveredsOfCurrentScript() |
清除当前脚本的所有已送达通知 |
removeAllPendingsOfCurrentScript() |
清除当前脚本的所有待发送通知 |
setBadgeCount(count) |
设置应用图标的角标数值 |
当脚本是通过点击通知启动时,可以通过 Notification.current 获取上下文信息:
NotificationRequest 字段| 字段名 | 说明 |
|---|---|
identifier |
通知请求的唯一标识符 |
content.title |
通知标题 |
content.subtitle |
通知副标题 |
content.body |
通知正文 |
content.userInfo |
附加信息 |
content.threadIdentifier |
分组标识 |
trigger |
触发器对象,控制通知的投递逻辑 |
以下示例展示了通知的完整用法:自定义 UI、交互按钮、点击行为、重复触发等。
notification.tsxScripting 中的 Notification API 提供了强大的本地通知功能:
tapAction 自定义点击通知的行为notification.tsx 创建富交互通知界面建议使用新的 trigger 和 tapAction 模式替代已废弃的 triggerTime 和 avoidRunningCurrentScriptWhenTapped。